package sy.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSON;

import sy.model.User;
import sy.service.UserServiceI;

@Controller
@RequestMapping("/userController")
public class UserController {
	protected Logger log = Logger.getLogger(this.getClass());
	@Autowired
	private UserServiceI userService;

	// 文件上传
	@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
	public String uploadFile(HttpServletRequest request) throws IOException {
		MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
		MultipartFile file = multipartHttpServletRequest.getFile("file");
		String fileName = file.getOriginalFilename();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		String path = request.getSession().getServletContext().getRealPath("/") + "upload/" + simpleDateFormat.format(new Date()) + fileName.substring(fileName.lastIndexOf("."));
		FileOutputStream fileOutputStream = new FileOutputStream(path);
		log.info(path);
		fileOutputStream.write(file.getBytes());
		fileOutputStream.flush();
		fileOutputStream.close();
		return "success";
	}

	@RequestMapping("/uploadFileUI")
	public String uploadFileUI() {
		return "uploadFile";
	}

	// redirect
	@RequestMapping("/redirect")
	public String redirect() {
		return "redirect:success.do";
	}

	@RequestMapping("/getPerson")
	public void getPerson(String name, PrintWriter printWriter, HttpServletRequest request) {
		log.info(name);
		log.info(request.getCharacterEncoding());
		printWriter.write(name + " 中文");
	}

	// the parameter was converted in initBinder
	@RequestMapping("/date")
	@ResponseBody
	public String date(Date date) {
		log.info(date);
		return "hello";
	}

	// At the time of initialization,convert the type "String" to type "date"
	@InitBinder
	public void initBinder(ServletRequestDataBinder binder) {
		binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
	}

	@RequestMapping("/success")
	@ResponseBody
	public String success(String name, String gender) {
		log.info(name + "\t" + gender);
		return "success";
	}

	@RequestMapping("/form")
	public String form() {
		return "form";
	}

	// RESTFul风格的SringMVC
	@RequestMapping("/showUser/{id}")
	public String showUser(@PathVariable Integer id, HttpServletRequest request) {
		User user = userService.getUserById(id);
		request.setAttribute("user", user);
		return "showUser";
	}

	@RequestMapping(value = "/getUserJson")
	@ResponseBody
	public String getUserJson() {
		List<User> users = userService.selectAll();
		return JSON.toJSONStringWithDateFormat(users, "yyyy-MM-dd HH:mm:ss");
	}

	@RequestMapping(value = "/addUser")
	@ResponseBody
	public String addUser(User user) {
		log.info(JSON.toJSON(user));
		return JSON.toJSONStringWithDateFormat(user, "yyyy-MM-dd HH:mm:ss");
	}
}
